package com.wxy.customview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.*
import android.widget.LinearLayout
import android.widget.Scroller
import com.wxy.customview.ext.dp
import kotlin.math.abs

/**
 * desc   :
 * author : xiangyuan.wang
 * date   : 2020-07-31
 * 大小 ：固定大小
 * 绘制 ：每个大刻度宽度为6  小刻度宽度为4  刻度间距4
 *
 *
 *
 *
 */
class BooheeRule @JvmOverloads constructor(context: Context, attributes: AttributeSet? = null, defStyleAttr: Int = 0)
    : LinearLayout(context, attributes, defStyleAttr) {
    private val TAG: String = "BooheeRule"
    private var mCenterY: Int = 0
    private val mLargeScaleWidth = 6.dp
    private val mLargeScaleHeight = 30.dp
    private val mScroller: Scroller = Scroller(context)
    private var mDownX: Float = 0f
    private val mBooheeScale: BooheeScale
    private val mScaledMaximumFlingVelocity: Int
    private val mScaledMinimumFlingVelocity: Int
    private var mVelocityTracker: VelocityTracker? = VelocityTracker.obtain()

    val centerScalePaint = Paint().apply {
        color = Color.RED
        isAntiAlias = true
        strokeWidth = mLargeScaleWidth.toFloat()
        strokeCap = Paint.Cap.ROUND
    }

    init {
        setPadding(4.dp, paddingTop, paddingRight, paddingBottom)
        mBooheeScale = BooheeScale(context)
        addView(mBooheeScale, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
        mScaledMaximumFlingVelocity = ViewConfiguration.get(context).scaledMaximumFlingVelocity
        mScaledMinimumFlingVelocity = ViewConfiguration.get(context).scaledMinimumFlingVelocity
    }

    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        canvas?.drawLine((width / 2).toFloat(), (mBooheeScale.height - mLargeScaleHeight).toFloat(), (width / 2).toFloat(), mBooheeScale.height.toFloat(), centerScalePaint)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        mVelocityTracker = mVelocityTracker ?: VelocityTracker.obtain()
        mVelocityTracker?.addMovement(event)
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                if (!mScroller.isFinished) {
                    mScroller.abortAnimation()
                }
                mDownX = event.x
            }
            MotionEvent.ACTION_MOVE -> {
                val diffX = event.x - mDownX
                mBooheeScale.scrollBy((-diffX).toInt(), 0)
                mDownX = event.x
            }
            MotionEvent.ACTION_UP -> {
                mVelocityTracker?.let {
                    it.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity.toFloat())
                    if (abs(it.xVelocity) > mScaledMinimumFlingVelocity) {
                        Log.d(TAG, "fling: ${mBooheeScale.scrollX}")

                        mScroller.fling(mBooheeScale.scrollX, mBooheeScale.scrollY, it.xVelocity.toInt(),
                                0, 0, Int.MAX_VALUE, 0, 0)
                        invalidate()
                    }
                }
                mVelocityTracker?.recycle()
                mVelocityTracker = null
            }
        }
        return true
    }

    override fun computeScroll() {
        super.computeScroll()
        if (mScroller.computeScrollOffset()) {
            Log.d(TAG, "computeScroll: ${mScroller.currX}")
            mBooheeScale.scrollTo(-mScroller.currX, mScroller.currY)
            invalidate()
        }
    }

}